package com.lyw.GeoLocationClient.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.*;
import com.baidu.mapapi.model.LatLng;
import com.lyw.GeoLocationClient.R;
import com.lyw.GeoLocationClient.utils.LoginCallback;
import com.lyw.GeoLocationClient.service.LBSClient;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


public class RadarActivity extends Activity {

    // 定位相关
    private MyLocationConfiguration.LocationMode mCurrentMode;
    BitmapDescriptor mCurrentMarker;

    MapView mMapView;
    BaiduMap mBaiduMap;
    private LBSClient mClient;
    private BDLocation mLastlocation;

    // UI相关
//    Button requestLocButton;
    boolean isFirstLoc = true;// 是否首次定位

    private LoginCallback uiCallback = new LoginCallback() {
        @Override
        public void onRegister(boolean success) {

        }

        @Override
        public void onLogin(boolean success) {

        }

//        @Override
//        public void onReceiveLocation(BDLocation location) {
//            // map view 销毁后不在处理新接收的位置
//            if (location == null || mMapView == null)
//                return;
//            mBaiduMap.clear();
//            MyLocationData locData = new MyLocationData.Builder()
//                    .accuracy(location.getRadius())
//                    .direction(location.getDirection()).latitude(location.getLatitude())
//                    .longitude(location.getLongitude()).build();
//                mBaiduMap.setMyLocationData(locData);
//                if (mClient.isOutOfGeofance()) {
//                NotificationHelper.getInstance(getApplication()).notifyOutOfGeofance(mClient.getGeoId());
//                isNotify = true;
//            } else {
//                if (isNotify) {
//                    isNotify = false;
//                    NotificationHelper.getInstance(getApplication()).removeNotify(mClient.getGeoId());
//                }
//            }
//            if (mClient.getLocationList().size() > 1) {
//                drawTrack(mClient.getLocationList());
//            }
//            if (isFirstLoc) {
//                isFirstLoc = false;
//                LatLng ll = new LatLng(location.getLatitude(),
//                        location.getLongitude());
//                MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 18.2f);
//                mBaiduMap.animateMapStatus(u);
//            }
//
//            drawGeofance();
//        }
    };

    private void drawGeofance() {
        LinkedList<LatLng> geofanceList = mClient.getGeofanceList();
        if (geofanceList.size() > 2) {
            OverlayOptions ooPolygon = new PolygonOptions().points(geofanceList)
                    .stroke(new Stroke(geofanceList.size(), getResources().getColor(R.color.lightblue)))
                    .fillColor(getResources().getColor(R.color.lightbluealpha));
            mBaiduMap.addOverlay(ooPolygon);
        }
    }


    private boolean isNotify;
    private void drawTrack(LinkedList<BDLocation> locations) {
        List<LatLng> list = new ArrayList<LatLng>();
        for (BDLocation lo : locations) {
            LatLng latLng = new LatLng(lo.getLatitude(), lo.getLongitude());
            list.add(latLng);
        }

        boolean isOut = mClient.isOutOfGeofance();

        int color = getResources().getColor(R.color.darkgreen);
        if (isOut) {
            color = getResources().getColor(R.color.red);
        }
        OverlayOptions ooPolyline = new PolylineOptions().width(10)
                .color(color).points(list);
        mBaiduMap.addOverlay(ooPolyline);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        if (getActionBar() != null) {
            getActionBar().show();
        }
//        requestLocButton = (Button) findViewById(R.id.button1);
        mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;

//        requestLocButton.setText("普通");
//        View.OnClickListener btnClickListener = new View.OnClickListener() {
//            public void onClick(View v) {
//                switch (mCurrentMode) {
//                    case NORMAL:
//                        requestLocButton.setText("跟随");
//                        mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING;
//                        mBaiduMap
//                                .setMyLocationConfigeration(new MyLocationConfiguration(
//                                        mCurrentMode, true, mCurrentMarker));
//                        break;
//                    case COMPASS:
//                        requestLocButton.setText("普通");
//                        mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;
//                        mBaiduMap
//                                .setMyLocationConfigeration(new MyLocationConfiguration(
//                                        mCurrentMode, true, mCurrentMarker));
//                        break;
//                    case FOLLOWING:
//                        requestLocButton.setText("罗盘");
//                        mCurrentMode = MyLocationConfiguration.LocationMode.COMPASS;
//                        mBaiduMap
//                                .setMyLocationConfigeration(new MyLocationConfiguration(
//                                        mCurrentMode, true, mCurrentMarker));
//                        break;
//                }
//            }
//        };
//        requestLocButton.setOnClickListener(btnClickListener);

        mClient = LBSClient.getInstance();
        mClient.setLoginCallback(uiCallback);

        // 地图初始化
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
                mCurrentMode, true, mCurrentMarker));
    }

    private static final int START = 1005;
    private static final int STOP = START+1;
    private static final int LOGOUT = STOP+1;
    private Menu mMenu;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuItem menuItem = menu.add(START, START, 1, R.string.start);
        // menuItem.setIcon(R.drawable.ic_launcher); //高版本中不建议添加图标。添加了图标也不会显示的。
        MenuItem menuItem1 = menu.add(STOP, STOP, 2, R.string.stop);
        MenuItem menuItem2 = menu.add(LOGOUT, LOGOUT, 3, R.string.logout);

        menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        menuItem1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        menuItem2.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
        mMenu = menu;
        mMenu.setGroupVisible(START, false);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case START:
                mMenu.setGroupVisible(STOP, true);
                mMenu.setGroupVisible(START, false);

                mClient.restartLocation();
                break;
            case STOP:
                mMenu.setGroupVisible(STOP, false);
                mMenu.setGroupVisible(START, true);

                mClient.stopLocation();
                isFirstLoc = false;

                break;
            case LOGOUT:
                mClient.logout();
                if (isNotify) {
                    isNotify = false;
                    NotificationHelper.getInstance(this).removeNotify(mClient.getGeoId());
                }
                this.finish();
            default:
                break;
        }
        return true;
    }


    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        mMapView.onResume();

        if (mClient.getCurrLocation() != null) {
            BDLocation location = mClient.getCurrLocation();
            if (mMapView != null) {
                MyLocationData locData = new MyLocationData.Builder()
                        .accuracy(location.getRadius())
                                // 此处设置获取到的方向信息，顺时针0-360
                        .direction(100).latitude(location.getLatitude())
                        .longitude(location.getLongitude()).build();
                mBaiduMap.setMyLocationData(locData);
                if (isFirstLoc) {
                    isFirstLoc = false;
                    LatLng ll = new LatLng(location.getLatitude(),
                            location.getLongitude());
                    MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 18.2f);
                    mBaiduMap.animateMapStatus(u);
                }
            }
        }
        mClient.restartLocation();

        super.onResume();
    }

    @Override
    protected void onDestroy() {
        // 关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
//        mClient.setLoginCallback(null);
        super.onDestroy();
    }

	
}
